home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Learn Microsoft Visual Basic 6.0 Now
/
Learn Microsoft Visual Basic 6.0 Now (Microsoft Press)(X03-58607)(1998).ISO
/
media
/
chap01
/
b01d025.cc2
< prev
next >
Wrap
Text File
|
1998-06-07
|
5KB
|
107 lines
0, In this demonstration, we'll complete
3, the Lucky Seven slot machine by writing
5, program code. Program code is written in a
7, language called Visual Basic, which
9, follows special syntax rules we'll discuss
11, throughout this course. You get started
13, writing program code by double-clicking
15, the object you want to customize. Since
17, the user will operate our program by
19, clicking the Spin and End buttons, I'll
21, write program code for both these buttons.
24, And because these buttons will be
25, activated by a single click, I'll add the code
28, to a special click event procedure for
30, each button. When I double-click the End
32, button, Visual Basic displays the Code
37, window, a special editor for editing
39, program code. The Code window contains an
41, Object drop-down list box, so you can
46, select the object you want to work with. It
50, also contains a Procedure drop-down
52, list box, so you can select the event you
55, want to customize. Private, Sub, and End
61, Sub statements mark the beginning and
62, ending of a procedure, a block of code
65, associated with a particular object or
67, task. In this case, we're writing an event
70, procedure for the End button. So we'll
72, type a program statement called End. End
75, is a Visual Basic keyword that tells the
77, compiler to stop the program. Now that
80, we're finished with this simple event
81, procedure, we'll click the Close button and
84, prepare to write the code for the Spin
86, button, an event procedure that puts
89, numbers in the Spin windows, and if the user
92, sees a seven, displays a stack of
94, coins. We'll double-click the Spin button and
99, open the Command1_Click event procedure
101, in the Code window. This event
104, procedure will hide the coin stack, create three
106, random numbers, and check to see if one
108, of the numbers is a seven. If a seven
111, does appear, the procedure will display
113, the coin stack and sound a beep in the
115, speaker. Okay, the first line I'll type
118, sets the Image1object's Visible property
120, to False. After I type the object's name
126, and a period, Visual Basic displays a
128, list box with all the valid property
130, settings for the object. I can either select
132, an object from the list or type it out
134, myself. When I type an equal sign, Visual
143, Basic displays another list box with
145, the value of the property setting. I can
148, type it out myself or select it from the
150, list. Finally, I'll add a comment
156, designated by a single quotation mark to
159, describe just what this programming statement
160, is doing. My comment, "Hide coins,"
166, won't be executed by the Visual Basic
167, compiler, but it's a note that I can look at
169, later when I wonder what the program is
171, doing. When I press the Enter key,
173, Visual Basic puts the comment in green type
176, to identify it as a comment that's not
178, run by the program. The False keyword is
181, also placed in blue type to identify it
183, as a keyword that Visual Basic uses.
186, Finally, Image1.Visible is in black type.
190, Now I'll take a moment to type the rest of
192, the program code for the event
193, procedure. The final Command1_Click event
198, procedure contains ten lines of code. It's a
201, little early in the programming course to
202, get into the specific details of each
203, of these program statements, but I'll
205, cover a few of the fine points if you'd
207, like. My goal is to get you familiar with
209, objects, properties, and the Code window
211, and to come back to program statement
213, syntax in Chapter 4. In the second, third
218, and fourth lines, I'm placing numbers in
219, the three Spinner windows in our form.
222, I created these with label objects
223, earlier, but now I'm assigning values to them
225, with program code. The numbers come
228, from a special Visual Basic function called
230, Rnd, which picks a random number
232, between zero and one. I multiply each number
235, by ten and use the Int function to create
238, an integer value between zero and nine
240, for the windows. Next, I check to see if
244, a seven appeared in one of the spinner
246, windows, using a decision structure
248, called If...Then that you'll learn more about
251, in Chapter 5. If a seven does appear in
257, one of the windows, procedure displays
259, the coin stack by setting the Image1
261, object's Visible property to True. To bring
264, home the point, I'll use the Beep
265, statement to sound a note from the computer
267, speaker. Now that I've finished typing the
270, program, I'll close the Code window and
275, use the Save Project As command to save
277, the project to disk.
281, END